package com.han.hotelplat.comm.utils;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.jar.JarFile;
import java.util.zip.ZipFile;

/**
 * Created by lkm on 17/4/12.
 */

public class IOHelp {

    /**
     *
     * @param mInputStream 会被关闭
     * @param charset
     * @return
     * @throws IOException
     */
    public static StringBuilder parserInputStreamToString(InputStream mInputStream, String charset) throws IOException {
        BufferedReader br=null;
        InputStreamReader isr=null;
        StringBuilder conteString=null;
        try{
             br=new BufferedReader(isr=new InputStreamReader(mInputStream,charset));//内容读取

            while(br.ready()){
                if(conteString==null){
                    conteString =new StringBuilder();
                }else {
                    conteString.append("\n");
                }
                conteString.append(br.readLine());//读取一行
            }


        } finally {
            IOHelp.close(br);
            IOHelp.close(isr);
            IOHelp.close(mInputStream);
        }
        return conteString==null?new StringBuilder():conteString;
    }

    /**
     * 读取字节直到byte[]满了，或者到没得读。
     * @param is
     * @param outbyte
     * @return 一共读到多少个字节。
     * @throws IOException
     */
    public static int readByteFull(InputStream is,byte[] outbyte) throws IOException {
        int curRead=0;//当前读了多少
        int readsize=0;//一共读了多少
        int residue=outbyte.length;//剩余
        while(true){
            curRead=is.read(outbyte, readsize,residue);
            if(curRead==-1)//一个都没读到，没有马上挑掉
                break;
            readsize+=curRead;//已经读取的大小
            residue=outbyte.length-readsize;
            if(residue==0)//已经写满byte[]。没剩余的
                break;
        }
        return readsize;
    }

    public static void close(Closeable c) {
        if(c!=null){
            if (c instanceof Flushable) {
                try {
                    ((Flushable) c).flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                c.close();
            } catch (Throwable e) {
//                e.printStackTrace();

                //                //android sdk 19是使用1.7的sdk的.
//                // 17 18的sdk是使用1.6的 目前发现在ZipFile类会在close的时候抛出异常IncompatibleClassChangeError
//                if(c instanceof ZipFile){
//                    try {
//                        ((ZipFile) c).close();
//                    } catch (IOException e1) {
//                        e1.printStackTrace();
//                    }
//                }else{
//                    throw  new IncompatibleClassChangeError();
//                }换成下面的方案
                try {
                    c.getClass().getMethod("close").invoke(c);
//                    System.out.println("=======IOHelp colse ok"+(c instanceof ZipFile));
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    e1.printStackTrace();
                } catch (NoSuchMethodException e1) {
                    e1.printStackTrace();
                } catch (Throwable e1) {
                    e1.printStackTrace();
                    if(e1 instanceof IncompatibleClassChangeError)
                        throw  new IncompatibleClassChangeError();
                }

            }
        }
    }// close end
}
